//
//  UserInfoModel.swift
//  SCA
//
//  Created by tjun on 2025/9/12.
//

import Foundation
import WCDBSwift

final class UserInfoModel: DatabaseEntity {
    
    var username: String = ""
    var userId: Int = 0
    var referrerCode: String = ""
    var level: Int = 0
    var levelIcon: String = ""
    var isSetReferrer: Bool = false
    var isSetPin: Bool = false
    var isSet2fa: Bool = false
    var forceSetPassword: Bool = false
    var forceSet2fa: Bool = false
    var canBind: Bool = false
    var canUnbind: Bool = false
    var backupMnemonic: Bool = false
    var factoryWallet: String = ""
    var evBalance: String = ""
    var ev: String = ""
    var totalRevenue: String = ""
    
    class var current: UserInfoModel {
        if let userInfoModel = AppDataStore.share.userInfoModel {
            return userInfoModel
        } else {
            if let userModel = UserInfoModel.getObject(where: UserInfoModel.Properties.userId == UserInfo.userId) {
                AppDataStore.share.userInfoModel = userModel
                return userModel
            } else {
                let model = UserInfoModel()
                model.userId = UserInfo.userId
                model.insertOrReplace()
                AppDataStore.share.userInfoModel = model
                return model
            }
        }
    }

    class func make(with dict: [String: Any]) -> UserInfoModel {
        let model = UserInfoModel.current
        model.username = dict.stringValue("username")
        model.userId = dict.intValue("uid")
        model.referrerCode = dict.stringValue("ref_code")

        if let config = dict["config"] as? [String: Any],
           let levelIcon = config["rank_img"] as? String,
           let ev = config["ev"] as? String,
           let evBalance = config["ev_balance"] as? String,
           let level = config["rank"] as? Int {
            model.level = level
            model.levelIcon = levelIcon
            model.ev = ev
            model.evBalance = evBalance
        }

        model.backupMnemonic = dict.boolValue("backup_mnemonic")
        model.canBind = dict.boolValue("can_bind")
        model.canUnbind = dict.boolValue("can_unbind")
        model.isSetPin = !dict.boolValue("set_pin")
        model.isSet2fa = !dict.boolValue("set_2fa")
        model.isSetReferrer = !dict.boolValue("set_ref")
        model.forceSetPassword = dict.boolValue("force_set_password")
        model.forceSet2fa = dict.boolValue("force_set_2fa")
        model.factoryWallet = dict.stringValue("factory_wallet")
        return model
    }
    
    enum CodingKeys: String, CodingTableKey {
        typealias Root = UserInfoModel
        case username, userId, referrerCode, level, levelIcon, isSetPin, isSet2fa, isSetReferrer, forceSetPassword, forceSet2fa,
             canBind, canUnbind, backupMnemonic, factoryWallet, evBalance, ev, totalRevenue

        static let objectRelationalMapping = TableBinding(CodingKeys.self) {
            BindColumnConstraint(userId, isPrimary: true)
        }
    }
}

 
